import java.awt.image.WritableRaster;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * Date: 2025-03-04
 * Time: 22:52
 */
public class Main {
    public static void main(String[] args) {
        int[] arr = {1, 8, 6, 5, 2, 4, 8, 3, 7};
        System.out.println(maxArea(arr));

    }

    //自己写的
    public static int maxArea(int[] height) {
        int before = 0;
        int after = height.length - 1;
        int max = 0;
        while (before <= after) {
            int h = Math.min(height[before], height[after]);
            int area = h * (after - before);

            if (height[before] < height[after]) {
                before++;
            } else if (height[before] > height[after]) {
                after--;
            } else {
                before++;
                after--;
            }

            if (area > max) {
                max = area;
            }
        }
        return max;
    }

    //老师写的
    public int maxArea2(int[] height) {
        int left = 0, right = height.length - 1, ret = 0;
        while (left < right) {
            int v = Math.min(height[left], height[right])*(right-left);
            ret = Math.max(ret, v);
            if (height[left] < height[right]) {
                left++;
            } else {
                right--;
            }
        }
        return ret;
    }
}
